package com.uncp.comisiondeadmision.seguridad.controlador;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.uncp.comisiondeadmision.seguridad.beans.UsuarioSistemaBean;
import com.uncp.comisiondeadmision.seguridad.service.SeguridadService;

public class ServletUsuario extends HttpServlet {

	
	private static final long serialVersionUID = 1L;
	
	SeguridadService dao = new SeguridadService();
	
	protected void service(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException {
		
		String metodo= req.getParameter("metodo");
		
		if ("inserta".equals(metodo)){
			inserta(req,res);
			lista(req,res);
		}
		else if ("actualiza".equals(metodo)) {
			actualiza(req, res);
			lista(req, res);
		} 
		else if ("lista".equals(metodo)) {
			lista(req, res);
		}
		else if ("busca".equals(metodo)) {
			busca(req, res);
		}
		else {
			elimina(req, res);
			lista(req, res);
		}
	}
	
	protected void busca(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {

		UsuarioSistemaBean bean = null;
		try {
			bean= dao.obtenerpk(Integer.parseInt(req.getParameter("id")));
		} catch (Exception e) {
				e.printStackTrace();
		}
		req.setAttribute("usuario",bean);
		req.getRequestDispatcher("").forward(req, resp);
	}
	
	protected void lista(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {	
		ArrayList<UsuarioSistemaBean> lista = null;
		try {
			lista= dao.listarTodos();
		} catch (Exception e) {
			e.printStackTrace();
		}
		req.getSession().setAttribute("lista_usuario", lista);
		req.getRequestDispatcher("").forward(req, resp);
	}

	protected void inserta(HttpServletRequest req, HttpServletResponse arg1)throws ServletException, IOException {
		
		UsuarioSistemaBean bean = new UsuarioSistemaBean();
		
		bean.setStrId(req.getParameter("codigo"));
		bean.setStrPassword(req.getParameter("password"));
		bean.setStrTipo(req.getParameter("tipo"));
		bean.setStrNombres(req.getParameter("nombres"));
		bean.setStrApePat(req.getParameter("appaterno"));
		bean.setStrApeMat(req.getParameter("apmaterno"));
		bean.setStrSexo(req.getParameter("sexo"));
		
		try {
			dao.insertaUsuario(bean);	
		} catch (Exception e) {
				System.out.println(e);
		}
		
		
	}

	protected void actualiza(HttpServletRequest req, HttpServletResponse arg1)throws ServletException, IOException {
		try {
			UsuarioSistemaBean bean = new UsuarioSistemaBean();
			
			bean.setStrId(req.getParameter("codigo"));
			bean.setStrPassword(req.getParameter("password"));
			bean.setStrTipo(req.getParameter("tipo"));
			bean.setStrNombres(req.getParameter("nombres"));
			bean.setStrApePat(req.getParameter("appaterno"));
			bean.setStrApeMat(req.getParameter("apmaterno"));
			bean.setStrSexo(req.getParameter("sexo"));		
			
			dao.actualizaUsuario(bean);
		} catch (Exception e) {
					e.printStackTrace();
		}
	}
	 
	protected void elimina(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {

		try {
			dao.eliminaUsuario(Integer.parseInt(req.getParameter("id")));			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	
}
